<template>
    <div class="doubleBarChartBox" :id="id"></div>
</template>

<script>
    import echarts from "echarts";
    export default {
        name: "doubleBarChart",
        props: {
            id: {
                type: String
            },
            now: {
                type: Array,
                default: []
            },
            suggest: {
                type: Array,
                default: []
            }
        },
        methods: {
            axisLabelFormatter(params){
                var newParamsName = "";// 最终拼接成的字符串
                var paramsNameNumber = params.length;// 实际标签的个数
                var provideNumber = 7;// 每行能显示的字的个数
                var rowNumber = Math.ceil(paramsNameNumber / provideNumber);// 换行的话，需要显示几行，向上取整
                /**
                 * 判断标签的个数是否大于规定的个数， 如果大于，则进行换行处理 如果不大于，即等于或小于，就返回原标签
                 */
                // 条件等同于rowNumber>1
                if (paramsNameNumber > provideNumber) {
                    /** 循环每一行,p表示行 */
                    for (var p = 0; p < rowNumber; p++) {
                        var tempStr = "";// 表示每一次截取的字符串
                        var start = p * provideNumber;// 开始截取的位置
                        var end = start + provideNumber;// 结束截取的位置
                        // 此处特殊处理最后一行的索引值
                        if (p == rowNumber - 1) {
                            // 最后一次不换行
                            tempStr = params.substring(start, paramsNameNumber);
                        } else {
                            // 每一次拼接字符串并换行
                            tempStr = params.substring(start, end) + "\n";
                        }
                        newParamsName += tempStr;// 最终拼成的字符串
                    }

                } else {
                    // 将旧标签的值赋给新标签
                    newParamsName = params;
                }
                //将最终的字符串返回
                return newParamsName;
            },
            numFormatter(q){
                return `${q.data.toFixed(2)}%`
            },
            getOption(){
                let option = {
                    xAxis: [
                        {
                            type: "category",
                            axisLine: {
                                show: false //x轴线消失
                            },
                            axisTick: {
                                show: false // x轴坐标点消失
                            },
                            axisLabel: {//坐标轴刻度标签的相关设置。
                                formatter:(p)=>this.axisLabelFormatter(p)
                            },
                            data: ["66%的概率下收益率不低于", "95%的概率下收益率不低于"]
                        }
                    ],
                    yAxis: [
                        {
                            type: "value",
                            axisLine: {
                                show: false //x轴线消失
                            },
                            axisTick: {
                                show: false //y轴坐标点消失
                            },
                            splitLine: {
                                lineStyle: {
                                    color: "#f2f2f2"
                                }
                            },
                            axisLabel: {
                                formatter: "{value}%",
                                textStyle: {
                                    color: "#91959E ",
                                    fontSize: 10
                                }

                            }
                        }
                    ],
                    series: [
                        {
                            name: "当前",
                            type: "bar",
                            data: this.now,
                            barWidth: "10%",
                            barGap: 0.8,
                            itemStyle: {
                                barBorderRadius: 3,
                                color: "#DEECFC"
                            },
                            label: {
                                show: true,
                                position: "top",
                                color: "#545861",
                                formatter:(q)=>this.numFormatter(q),

                                align: "right",
                                offset: [5, 2]
                            }
                        },
                        {
                            name: "建议",
                            type: "bar",
                            data: this.suggest,
                            barWidth: "10%",
                            barGap: 0.8,
                            itemStyle: {
                                barBorderRadius: 3,
                                color: "#5BA0F0"
                            },
                            label: {
                                show: true,
                                position: "top",
                                color: "#545861",
                                formatter: (q)=>this.numFormatter(q),
                                align: "left",
                                offset: [-5, 2]
                            }
                        }
                    ]
                };
                return option;
            },
            showChart(){
                let myChart = echarts.init(document.getElementById(this.id));
                myChart.setOption(this.getOption());
                window.addEventListener("resize", () => {myChart.resize();});
            }
        },
        mounted() {
            this.showChart()
        }
    };
</script>

<style scoped>

</style>
